<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<?php

/*
 *  A Part of snock project
 *   Author king-of-war
 * Last modified 2009-5-12
 *  designed for sort operation
 */
class cfg {
	private $root_path;
	private $error;
	function __construct($url) {
		/*
		* load connection class
				 */
		include_once $url . 'includes/conn.php';
		/*
		 * load xml class
		 */
		include_once $url . 'includes/xml/xml_op.php';
		/*
		* load error report class
		*/
		include_once $url . 'admin/class/error.class.php';
		$error = new Errmsg();
		$this->set_error($error);
		$this->set_root_path($url);
	}
	/*
	 * select the date from database
	 */
	function cfg_get() {
		$conn = new conn();
		$sql = 'select * from config';
		$conn->set_sql($sql);

		$line = $conn->get_line();
		$cfgs = array ();
		if ($line) {
			$result = $conn->get_result();
			$row = $result->fetch_assoc();
			$cfg['site_name'] = $row['site_name'];
			$cfg['meta_key'] = $row['meta_key'];
			$cfg['meta_des'] = $row['meta_des'];
			$cfg['url'] = $row['url'];
			$cfg['temp'] = $row['temp'];
			$cfg['close'] = $row['close'];
			$cfg['close_info'] = $row['close_info'];
			$cfg['page_num'] = $row['page_num'];
			$cfg['guest'] = $row['guest'];
			$cfg['copyright'] = $row['copyright'];
		} else {
			echo 'Error: Config data lost';
		//	exit;
		}

		return $cfg;
	}
	/*
	 * update site config data
	 */
	function cfg_updata($site_name, $meta_key, $meta_des, $url, $temp, $close, $close_info, $page_num,$guest, $copyright) {
		$conn = new conn();
		$line = 0;
		$sql = sprintf("update config set site_name='%s',meta_key='%s',meta_des='%s',url='%s',temp='%s',close='%s',close_info='%s',page_num='%s',guest='%d',copyright='%s'", $site_name, $meta_key, $meta_des, $url, $temp, $close, $close_info, $page_num,$guest, $copyright);
		$conn->set_sql($sql);
		$line = $conn->get_line();
		if ($line) {
			$this->xml_write($site_name, $meta_key, $meta_des, $url, $temp, $close, $close_info, $page_num,$guest, $copyright);
		}
		return $line;
	}
	/*
	 * write to xml
	 */
	function xml_write($site_name, $meta_key, $meta_des, $url, $temp, $close, $close_info, $page_num,$guest, $copyright) {

		$url = $this->get_root_path();
		$xml_op = new xml_op($url);
		$file = 'includes/xml/config.xml';
		if (file_exists($url . $file)) {

			$xml = simplexml_load_file($url . $file);

			/*
			 * write to xml
			 */

			$xml->config[0]->site_name = $site_name;
			$xml->config[0]->meta_key = $meta_key;
			$xml->config[0]->meta_des = $meta_des;
			$xml->config[0]->url = $url;
			$xml->config[0]->temp = $temp;
			$xml->config[0]->close = $close;
			$xml->config[0]->close_info= $close_info;
			$xml->config[0]->page_num = $page_num;
			$xml->config[0]->guest = $guest;
			$xml->config[0]->copyright = $copyright;

			$contant = $xml->asXML();

			$xml_op->xml_creat($file, $contant);

			return true;
		} else {
			echo 'File open error <br/>Check the file please';
			return false;
			exit;
		}

	}

	/*
	 * read xml
	 */
	function xml_read($key) {

		$url = $this->get_root_path();
		$xml_op = new xml_op($url);
		$file = 'includes/xml/config.xml';
		if (file_exists($url . $file)) {

			$xml = simplexml_load_file($url . $file);

			/*
			 * read xml
			 */

		$result=$xml->config[0]->$key;

			return $result;
		} else {
			echo 'File open error <br/>Check the file please';
			return null;
			exit;
		}


	}
	/*
	 * scan directory for get templates num
	 *
	 */
	function temp($root) {
		$root_path = $this->get_root_path();
		$root = $root_path . $root;
		if (!empty ($root)) {
			$files = scandir($root, 0);

			/*
			* remove ./  ../
			*/
			array_shift($files);
			array_shift($files);
			$result = $files;
		} else {

			echo 'Error: root dir null ';
			$result = null;
		}
		return $result;

	}

	/*
	 * function for vars which used above
	 */
	function set_root_path($url) {
		$this->root_path = $url;
	}
	function get_root_path() {
		return $this->root_path;
	}
	function set_error($error) {
		$this->error = $error;
	}
	function get_error() {
		return $this->error;
	}
}
?>
